<template>
	<view class="customer">
		<view class="search-box">
			<uni-icons type="search" size="24" color="#D2D3DA"></uni-icons>
			<input v-model="keyword" placeholder-style="color: #B1B0B3;" placeholder="请输入客户名称" @input="filterbyKeyword" />
		</view>
		<view class="container">
			<view class="loading" v-if="pagingState.loading">
				<image mode="widthFix" src="@/static/image/loading.gif"></image>
			</view>
			<view class="no-person" v-else-if="customerList.length === 0">
				暂无客户
			</view>
			<template v-else>
				<uni-list class="list">
					<uni-list-item v-for="item of customerList" :clickable="true" link @click="itemClick(item)" :key="item.id">
						<template v-slot:header>
							<view class="avatar-box">
								<image :src="item.avatarUrl ? item.avatarUrl : getDefaultImage()"
									@error="item.avatarUrl = getDefaultImage()" mode="aspectFill"></image>
							</view>
						</template>
						<template v-slot:body>
							<view class="content">
								<view class="row">
									<text class="name">{{item.name || ''}}</text>
								</view>
								<view class="row">
									<view class="mobile">
										<image src="@/static/image/phone.png"></image>
										<text>{{item.mobile || '暂未设置'}}</text>
									</view>
								</view>
								<view class="row">
									<text class="company_name">{{item.company_name || '暂未设置'}}</text>
								</view>
							</view>
						</template>
					</uni-list-item>
				</uni-list>
				<view class="loading_more" v-if="pagingState.loading_more">
					<text>加载中...</text>
					<image mode="widthFix" src="@/static/image/loading.gif"></image>
				</view>
				<view class="noMore" v-if="!pagingState.hasMore">
					<text>没有更多了~</text>
				</view>
			</template>
		</view>
		<view class="addFloatBadge" @click="toAdd" v-if="type !== 'previewRecord'">
			<image mode="widthFix" src="@/static/image/customer-add.png"></image>
		</view>
	</view>
</template>

<script>
	import {
		getCustomerList,
	} from "@/api/search_read.js";
	import _ from "lodash";
	export default {
		data() {
			return {
				type: '',
				keyword: '',
				// 客户列表
				customerList: [],
				// 分页状态
				pagingState: {
					// 页码
					page: 1,
					// 每页条数
					limit: 20,
					// 总条数
					total: 0,
					// 还有更多
					hasMore: true,
					// 加载中
					loading: true,
					// 加载更多中
					loading_more: false,
				}
			}
		},
		onPullDownRefresh() {
			this.refresh();
		},
		onReachBottom: _.debounce(async function() {
			// 加载中时不在发起加载
			if (this.pagingState.loading_more || !this.pagingState.hasMore) {
				return;
			}
			this.pagingState.loading_more = true;
			await this.getList();
			this.$nextTick(() => {
				this.pagingState.loading_more = false;
			})
		}, 1000, {
			leading: false,
			trailing: true
		}),
		onLoad(options) {
			this.type = options.type || '';
			this.refresh();
			uni.$on('customerChange', () => {
				this.refresh();
			})
		},
		onUnload() {
			uni.$off('customerChange');
		},
		methods: {
			// 刷新列表
			async refresh() {
				this.pagingState.page = 1;
				this.pagingState.total = 0;
				this.customerList = [];
				this.pagingState.hasMore = true;
				this.pagingState.loading = true;
				await this.getList();
				this.pagingState.loading = false;
				uni.stopPullDownRefresh();
			},
			// 获取客户列表
			async getList() {
				if (!this.pagingState.hasMore) {
					return Promise.reject(false);
				}
				return new Promise((resolve) => {
					getCustomerList({
						limit: this.pagingState.limit,
						offset: this.pagingState.limit * (this.pagingState.page - 1),
						keyword: this.keyword,
					}).then(res => {
						if (res.data?.result) {
							this.pagingState.total = res.data?.result?.length || 0;
							this.customerList = this.customerList.concat(res?.data?.result?.records.map(x => {
								x.company_name = x.company_id?.[1] || '';
								x.avatarUrl = x.avatar_256 ? this.image_base64(x.avatar_256) : '';
								return x;
							}));
							if (this.customerList.length === this.pagingState.total) {
								this.pagingState.hasMore = false;
							} else {
								this.pagingState.page++;
							}
						} else {
							uni.showModal({
								title: '错误提示',
								content: res?.data?.error?.data?.message || '获取客户列表失败',
							})
						}
					}).catch(() => {
						uni.showToast({
							icon: 'none',
							title: '系统错误',
						})
					}).finally(() => {
						resolve(true);
					})
				})
			},
			// 关键字过滤产品
			filterbyKeyword: _.debounce(function() {
				this.refresh();
			}, 500, {
				leading: false,
				trailing: true
			}),
			image_base64(photo) {
				return 'data:image/jpeg;base64,' + photo;
			},
			// 默认头像
			getDefaultImage() {
				return require("@/static/image/avatar-default.png");
			},
			itemClick: _.debounce(function(item) {
				if (this.type === 'previewRecord') {
					uni.navigateTo({
						url: `/pages/previewRecord/previewRecord?id=${item.id}`,
					})
				} else {
					uni.$emit('choosePartner', item);
					uni.navigateBack();
				}
			}, 300),
			// 前往添加客户
			toAdd: _.debounce(function() {
				uni.navigateTo({
					url: '/pages/customerEdit/customerEdit',
				})
			}, 300),
		},
	}
</script>

<style lang="scss" scoped>
	.customer {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;

		.search-box {
			width: 660rpx;
			margin: 32rpx 0;
			height: 70rpx;
			background-color: white;
			border-radius: 70rpx;
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 0 24rpx;

			uni-icons {
				margin-right: 10rpx;
				flex-shrink: 0;
			}

			input {
				flex: 1;
			}
		}

		.container {
			width: 100%;

			.loading,
			.no-person {
				width: 100%;
				height: 100vh;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: $uni-font-size-lg;

				image {
					width: 50rpx;
				}
			}

			.list {
				width: 100%;

				.avatar-box {
					margin: auto 0;
					width: 90rpx;
					height: 90rpx;
					border-radius: 90rpx;
					overflow: hidden;
					flex-shrink: 0;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.content {
					width: 100%;
					display: flex;
					flex-direction: column;
					justify-content: center;
					padding-left: 30rpx;

					.row {
						width: 100%;

						&:not(:first-child) {
							margin-top: 16rpx;
						}

						display: flex;
						align-items: flex-start;

						.name {
							flex: 1;
							font-size: 32rpx;
							color: #34353A;
							word-break: break-all;
							line-height: 32rpx;
						}

						.mobile {
							display: flex;
							align-items: center;

							image {
								width: 30rpx;
								height: 30rpx;
								margin-right: 6rpx;
							}

							text {
								font-size: 28rpx;
								color: #B1B0B3;
								line-height: 32rpx;
							}
						}

						.company_name {
							font-size: 28rpx;
							color: #B1B0B3;
							line-height: 32rpx;
						}
					}
				}
			}

			.loading_more {
				width: 100%;
				padding: 10rpx 0;
				display: flex;
				justify-content: center;
				align-items: center;

				text {
					font-size: $uni-font-size-sm;
				}

				image {
					width: 30rpx;
					height: 30rpx;
					margin-left: 20rpx;
				}
			}

			.noMore {
				width: 100%;
				padding: 20rpx 0;
				display: flex;
				justify-content: center;
				align-items: center;

				text {
					font-size: $uni-font-size-sm;
				}
			}
		}
	}

	/deep/.uni-list-item__container {
		padding: 30rpx 32rpx;
	}

	.addFloatBadge {
		position: fixed;
		bottom: calc(80rpx + 50px + env(safe-area-inset-bottom));
		right: 50rpx;
		width: 120rpx;
		height: 120rpx;
		border-radius: 120rpx;
		background-color: #3F7CFB;
		display: flex;
		justify-content: center;
		align-items: center;
		opacity: .4;

		image {
			width: 56rpx;
		}
	}
</style>